package com.htfyun.ft.agingtest;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.htfyun.ft.BaseActivity;
import com.htfyun.ft.R;
import com.htfyun.ft.utils.DebugLog;
import com.htfyun.ft.utils.FileReadWrite;
import com.htfyun.ft.utils.executor.AppExecutors;

import java.io.File;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.core.app.ActivityCompat;

public class AgingDetailActivity extends BaseActivity {

    private ListView list;
    MyAdapter adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_aging_detail);

        View txtReturn = findViewById(R.id.txtReturn);
        if (txtReturn != null) {
            txtReturn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ActivityCompat.finishAfterTransition(AgingDetailActivity.this);
                }
            });
        }

        list = (ListView) findViewById(R.id.list);

        adapter = new MyAdapter(getContext(), -1, AgingTestItemAttr.getAgingDetailFiles());
        list.setAdapter(adapter);

        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                File file = adapter.getItem(position);
                AppExecutors.runOnIoThread(new MyRunnable(file));

            }
        });
    }


    private void showDialog(String msg){
        final AlertDialog.Builder dialog =
                new AlertDialog.Builder(getContext());

        final View dialogView = LayoutInflater.from(getContext())
                .inflate(R.layout.dialog_aging_detail, null);

        final TextView txt = (TextView) dialogView.findViewById(R.id.txt);
        txt.setText(msg);
        dialog.setView(dialogView);
        dialog.setPositiveButton(android.R.string.ok,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        // 显示
        dialog.show();
    }

    private final int MSG_SHOW_DIALOG = 100;
    @Override
    protected void handleMessage(Message msg) {

        switch (msg.what) {
            case MSG_SHOW_DIALOG:
                if (msg.obj instanceof String) {
                    showDialog((String) msg.obj);
                }
                break;
        }

    }

    private final class MyRunnable implements Runnable {
        private final File file;
        public MyRunnable(File file) {
            this.file = file;
        }

        @Override
        public void run() {
            String content = FileReadWrite.readFileByLines(file);
            getHandler().obtainMessage(MSG_SHOW_DIALOG, content).sendToTarget();
        }
    }

    private final class MyAdapter extends ArrayAdapter<File> {

        public MyAdapter(@NonNull Context context, int resource, @NonNull List<File> objects) {
            super(context, resource, objects);
        }

        @NonNull
        @Override
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView != null) {
                holder = (ViewHolder)convertView.getTag();
            } else {
                holder = new ViewHolder();
                LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(R.layout.item_aging_detail, null);
                holder.txt = (TextView) convertView.findViewById(R.id.txt);

                convertView.setTag(holder);
            }

            File file = getItem(position);
            if (file != null) {

                holder.txt.setText(file.getName());
            }
            return convertView;
        }
    }

    private final class ViewHolder {
        TextView txt;
    }
}
